package ScheduledThreadPoolExecutor;

// import org.junit.Test;

import java.util.Objects;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * @author wzg
 * @date 2021/7/5 22:51
 */
public class IntTest {

    private static final int COUNT_BITS=Integer.SIZE-3;
    /**
     * CAPACITY = 1 << COUNT_BITS 得到 1 000…0000（28 个 0） 1,0000,0000,0000,0000,0000,0000,0000
     * CAPACITY = CAPACITY-1 得到 1111…111 (28 个 1)  1111,1111,1111,1111,1111,1111,1111
     */
    private static final int CAPACITY   = (1 << COUNT_BITS) - 1;

    private static final int RUNNING    = -1 << COUNT_BITS;
    private static final int SHUTDOWN   =  0 << COUNT_BITS;
    private static final int STOP       =  1 << COUNT_BITS;
    private static final int TIDYING    =  2 << COUNT_BITS;
    private static final int TERMINATED =  2021<< COUNT_BITS;

    /**
     * -1,0000,0000,0000,0000,0000,0000,0000 (取反之后，高四位 为1 ，负数，因为int 为 32位)
     * 1 & 0 或者 0 & 0都为 0
     */
    private static int runStateOf(int c)     { return c & ~CAPACITY; }

    /**
     * 因此 c & CAPACITY， 就可以获取变量 c 的低 28 位的值，高三位 与运算结果 为 0
     */
    private static int workerCountOf(int c)  { return c & CAPACITY; }
    private static int ctlOf(int rs, int wc) { return rs | wc; }

    // @Test
    public void test(){
        int result=ctlOf(TERMINATED,1);

        result++;
        int runState=runStateOf(result);

        int workerCount=workerCountOf(result);

//        ScheduledThreadPoolExecutor
    }

    private static final int COUNT_BITS1 = Integer.SIZE - 16;
    private static final int MAX_COUNT = (1 << COUNT_BITS1) - 1;


    // @Test
    public void test1(){
        int result=generateValue(2,1);

        result++;
        int runState=getPartNumber(result);

        int workerCount=getPartCount(result);

//        ScheduledThreadPoolExecutor
    }
    private int getPartNumber(Integer c) {
        if(Objects.isNull(c))return 0;
        return c & MAX_COUNT;
    }

    private int getPartCount(Integer c) {
        if(Objects.isNull(c))return 0;
        return (c & ~MAX_COUNT)>>COUNT_BITS1;
    }

    private int generateValue(int partCount,int partNumber) {
        return (partCount << COUNT_BITS1) | partNumber;
    }
}
